//
//  QRCodeViewController.m
//  SmartLife
//
//  Created by Lawrence on 15/10/14.
//  Copyright © 2015年 Lawrence. All rights reserved.
//

#import "QRCodeViewController.h"
#import <ZBarSDK.h>
#import <ZBarReaderView.h>
@interface QRCodeViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property ZBarReaderView *readerView;
@property ZBarImageScanner *scanner;

@end
@implementation QRCodeViewController
@synthesize readerView;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化导航栏
    [self initNavItem];
    [self initZbarReaderView];
}

-(void)initZbarReaderView{
    
    readerView=[ZBarReaderView new];
    readerView.frame=CGRectMake(20,(self.view.frame.size.height-(self.view.frame.size.width-40))/2,self.view.frame.size.width-40,self.view.frame.size.width-40);
    readerView.readerDelegate=self;
    [self.view addSubview:readerView];
    readerView.torchMode=0;
    
    _scanner=readerView.scanner;
    
    
    [_scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
    
    if (TARGET_IPHONE_SIMULATOR) {
        ZBarCameraSimulator  *cameraSim=[[ZBarCameraSimulator alloc] initWithViewController:self];
        cameraSim.readerView=readerView;
    }

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated{
    [readerView start];  //开启二维码扫描
}
-(void)viewDidDisappear:(BOOL)animated{
    if(!TARGET_IPHONE_SIMULATOR)  //判断是否为模拟器
    [readerView stop];   //停止二维码扫描
}
-(void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image{
    ZBarSymbol *symbol=nil;
    for (symbol in symbols)
        break;
    //当获取到二维码信息时播放震动效果
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  //调用手机震动
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:symbol.data]];
    [self.readerView stop];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    id<NSFastEnumeration> results=[info objectForKey:ZBarReaderControllerResults];
    ZBarSymbol *symbol;
    for (symbol in results )
        break;
    //取得到条形码
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"扫描结果" message:symbol.data delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil, nil];
    [alert show];
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect) readerViewBounds{
    CGFloat x,y,width,height;
    
    x = rect.origin.x / readerViewBounds.size.width;
    y = rect.origin.y / readerViewBounds.size.height;
    width = rect.size.width / readerViewBounds.size.width;
    height = rect.size.height / readerViewBounds.size.height;
    
    return CGRectMake(x,y, width, height);
}
-(void)initNavItem{
    //self.navigationController.navigationItem.title=@"条形码／二维码";
    
    
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc] initWithTitle:@"相册" style:UIBarButtonItemStylePlain target:self action:@selector(initPhoto)];
    self.navigationItem.rightBarButtonItem=rightItem;
    
    
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"返回", nil) style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];
    
    
    self.navigationItem.leftBarButtonItem=leftItem;
}

#pragma mark 返回上一个界面
-(void)popViewController{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)initPhoto{
    UIImagePickerController *imagePicker=[[UIImagePickerController alloc] init];
    imagePicker.delegate=self;
    imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
    imagePicker.allowsEditing=YES;
    [self presentViewController:imagePicker animated:YES completion:^{
        
    }];
}
@end
